
<template>
  <el-menu
    :collapse="isCollapse"
    default-active="2"
    class="el-menu-vertical-demo"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b">
    <h2 v-show="!isCollapse">后台管理系统</h2>
    <h2 v-show="isCollapse">管理</h2>
    <el-menu-item v-for="item in noChildren" :key="item.path" :index="item.path" @click="switchMenu(item)">
      <i :class="'el-icon-'+ item.icon"></i>
      <span slot="title">{{item.label}}</span>
    </el-menu-item>
    <el-submenu 
      :index="item.path" v-for="(item, index) in hasChildren" :key="index" 
    >
      <template slot="title">
        <i :class="'el-icon-'+ item.icon"></i>
        <span slot="title">{{item.label}}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item 
          :index="subItem.path" v-for="(subItem,subIndex) in item.children" :key="subIndex"
          @click="switchMenu(subItem)"> 
      <i :class="'el-icon-'+ subItem.icon"></i>
      <span slot="title">{{subItem.label}}</span>
      </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>
<script>
export default {
  name: 'CommonAside',
  data() {
    return {
      asideMenu: [
        {
          path: '/',
          name: 'home',
          lable: '首页',
          icon: 'home'
        },
        {
          path: '/video',
          lable: '视频管理',
          name: 'video',
          icon: 'video'
        },
        {
          path: '/user',
          lable: '用户管理',
          name: 'user',
          icon: 'yonghuguanli_huaban'
        },
        {
          lable: '其他',
          icon: 'qita',
          children: [
            {
              path: '/page1',
              lable: '页面1',
              name: 'page1',
              icon: 'setting'
            },
            {
              path: '/page2',
              lable: '页面2',
              name: 'page2',
              icon: 'setting'
            },
          ]
        },

      ]
    }
  },
  methods: {
    switchMenu(curItem) {
      this.$router.push(curItem.path)
      this.$store.commit('selectMenu', curItem)
    }
  },
  computed: {
    isCollapse() {
      return this.$store.state.isCollapse
    },
    noChildren() {
      return this.menu.filter(item => !item.children)
    },
    hasChildren() {
      return this.menu.filter(item => item.children)
    },
    menu(){
       return this.$store.state.tab.menu
    }
  }
}
</script>
<style lang="scss" scoped>
  .el-menu {
    height: 100%;
    border: none;
    h2 {
      color: #fff;
      text-align: center;
      line-height: 59px;
      box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px, rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
      user-select: none;
    }
  }

  .el-menu-vertical-demo {
    border: none;
  }
// 菜单折叠动画优化使得流畅
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  
  svg {
    margin: -7px 10px -7px 0;
    width: 1.8em;
    height: 1.8em;
  }


</style>